import Node from './Node';

class Tree extends zrender.Group{
    constructor(data){
        super();
        this.data=data||{
          text:'根节点'
        };
        this.root = null;
        this.layoutList=[];
        this.induces=[];//用于存储所有的总结
        this.wireFrames=[];//用于存储所有的线框
        this.lineGroup=new zrender.Group();
        this.add(this.lineGroup);
        this.initNodes();
    }

    setRoot(node){
        this.root=node;
    }

    getRoot(){
        return this.root;
    }

    //深度遍历方法,可以指定node
    traverseDF(callback,node){
         // 一个递归，立即执行函数
        (function recurse(currentNode) {
            for (var i = 0, length = currentNode.childrens.length; i < length; i++) {
                recurse(currentNode.childrens[i]);
            }
            callback(currentNode);
            // 首先执行
        })(node||this.root);
    }

    //广度遍历方法，可以指定node
    traverseBF(callback,node){
        var array=[];
        array.push(node||this.root);
        var currentNode=array.shift();
        while(currentNode){
            for(let i=0,len=currentNode.childrens.length;i<len;i++){
                array.push(currentNode.childrens[i]);
            }
            callback(currentNode);
            currentNode=array.shift();
        }
    }

    //新增节点，可以指定位置
    addNode(child,parent,index){
        if(parent){
            parent.addChild(child,index);
            if(!parent.isExpand()){
                 child.hide();
            }
        }else{
            this.root=child;
        }
        this.add(child);
        child.setMinder(this);
    }

    //删除节点
    removeNode(node){
        if(node&&node.getParent()){
            var index=node.getParent().removeChild(node);
            this.traverseBF((n)=>{
              this.remove(n);
              if(n.induce){
                  this.removeInduce(n.induce);
              }
            },node);
            return index;
        }
    }

    initNodes(){
          var me=this;
          (function initNode(data,parent) {
                var node=new Node(data);
                me.addNode(node,parent);
                node.setMinder(me);
                data.children&&data.children.forEach((d)=>{
                    initNode(d,node);
                })
           })(this.data,null);
      }

      getNodeById(id){
          var node=null;
          if(id){
              this.traverseBF((n)=>{
                  if(n.getId()==id){
                      node=n;
                  }
              });
          }
          return node;
      }

      //注册布局的方法
      registerLayout(key,layout){
          this.layoutList.push({
              key:key,
              layout:layout,
              use:false
          })
      }

      //切换布局
      useLayout(key){
          this.layoutList.forEach((item)=>{
               if(item.key==key){
                   item.use=true
               }else{
                   item.use=false
               }
           });
      }

      //获取使用的某个布局
      getUseLayout(){
           var list=this.layoutList.filter((item)=>{
               return item.use;
           });
           return list[0]&&list[0].layout
      }

      //获取布局列表
      getLayoutList(){
         return this.layoutList;
      }

      //布局方法，direct代表 如果只想布局在某一侧，如'left','right'
      layout(node,direct){
          this.getUseLayout().layout(node,direct);
      }

      //刷新方法
      refresh(){
          var direct= this.getUseLayout().direct;
          this.layout("",direct);
          this.induces.sort(function(a,b){
              return b.node.getLevel()-a.node.getLevel();
          }).forEach((induce)=>{
              induce.refresh();
          });
          this.wireFrames.forEach((wf)=>{
              wf.refresh();
          });
      }

      //一个清空连线的方法
      removeAllLine(){
          this.lineGroup.removeAll();
      }

      //添加连线的方法
      addLine(line){
          this.lineGroup.add(line);
      }

      //取消选择
      clearSelectNode(){
          this.traverseBF((node)=>{
              node.unSelect();
          });
      }

      //选择某节点
      selectNode(node){
          node.select();
      }

      //获取选择的节点
      getSelectNodes(){
          var nodes=[];
           this.traverseBF((node)=>{
              if(node.selected){
                  nodes.push(node);
              }
          });
          return nodes;
      }

      //设置位置
      setPosition(x,y){
        this.attr({
          position:[x,y]
        })
      }

      getPosition(){
        return {
          x:this.position.slice()[0],
          y:this.position.slice()[1]
        }
      }

      zoom(type){
        var scale=this.scale.slice()[0];
        if(type=='up'){
            if(scale<=3){//每次增加百分之10
              this.attr({
                  scale:[scale+0.1,scale+0.1]
              });
            }
        }else{
            if(scale>=0.1){
               this.attr({
                  scale:[scale-0.1,scale-0.1]
              });
            }
        }
    }
    
    addInduce(induce){
        this.induces.push(induce);
        this.add(induce);
    }

    removeInduce(induce){
        var index=this.induces.indexOf(induce);
        if(index>-1){
            this.induces.splice(index,1);
            this.remove(induce);
        }
        return index;
    }

    addWireFrame(wf){
        this.wireFrames.push(wf);
        this.add(wf);
    }

    removeWireFrame(wf){
        var index=this.wireframes.indexOf(wf);
        if(index>-1){
            this.wireframes.splice(index,1);
            this.remove(wf);
        }
        return index;
    }

}


export { Tree as Minder };
